package com.youbug.system.controller;

import cn.hutool.crypto.symmetric.AES;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.youbug.system.entity.User;
import com.youbug.system.service.IUserService;
import com.youbug.common.vo.R;
import com.youbug.system.vo.UserVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private IUserService userService;
    @Autowired
    private AES aes;

    @PostMapping("/getUsers")
    public R<IPage<User>> getUser(@RequestBody UserVo userVo){

        IPage<User> userPage = userService.getUserPage(userVo);

        return R.ok(userPage);
    }
    @GetMapping("/getUser/{id}")
    public R<User> getUser(@PathVariable String  id){

        User user = userService.getById(id);

        return R.ok(user);
    }

    @PostMapping("/add")
    public R<Boolean> add(@RequestBody User user){

        user.setPassword(aes.encryptBase64("111111"));

        boolean save = userService.save(user);

        return  R.ok(save);
    }
    @GetMapping("/delete/{id}")
    public R<Boolean> delete(@PathVariable String id){

        boolean remove = userService.removeById(id);

        return  R.ok(remove);
    }

    @PostMapping("/update")
    public R<Boolean> update(@RequestBody User user){

        boolean save = userService.updateById(user);

        return  R.ok(save);
    }

    @PostMapping("/importUsers")
    public R<Boolean> importUsers(MultipartFile file){

        boolean imported = userService.importUsers(file);

        return  R.ok(imported);
    }
}
